#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
# @FileName  :security.py
# @Time      :2022/4/28 3:18 PM
# @Author    :Toliong toliong@foxmail.com
"""
from datetime import datetime, timedelta
from typing import Optional
from jose import JWTError, jwt


from core.config import settings


def create_access_token(data:dict, expires_delta: Optional[timedelta] = None):
    to_encode = data.copy()
    if expires_delta:
        expire = datetime.utcnow() + expires_delta
    else:
        expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
    to_encode.update({"exp": expire})
    encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
    return encoded_jwt

if __name__ == "__main__":
    pass
